//index.js
const app = getApp()
Page({
    data: {
        motto: 'Hello World',
        userInfo: {},
        _num: 0,
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo')
    },
    onLoad: function() {
        var that = this;
        that.getUserInfo(function () {
            wx.request({
                url: 'https://www.mayizhihuan.com/Home/Nav/',
                data: {
                    PHPSESSID: wx.getStorageSync('sid')
                },
                method: 'GET',
                header: {
                    'content-type': 'application/json'
                },
                success: function (res) {
                    that.setData({
                        txtArray: res.data.datas
                    })
                }
            }),
                wx.request({
                    url: 'https://www.mayizhihuan.com/Home/Adlittle/',
                    data: {
                        PHPSESSID: wx.getStorageSync('sid')
                    },
                    method: 'GET',
                    header: {
                        'content-type': 'application/json'
                    },
                    success: function (res) {
                        var ad_img = res.data.datas;
                        var cid;
                        for (var i = 0; i < ad_img.length; i++) {
                            if (ad_img[i].ad_id == 27) {
                                cid = ad_img[i].ad_content
                            }
                        }
                        that.setData({
                            tup: cid
                        })
                    }
                })
            that.jiazai();
        })
    },
    onShow: function(options) {     
    },
    getUserInfo: function(cb) { // 通过sessionID获取用户登录状态, 返回用户信息
        var that = this;
        var sid = wx.getStorageSync('sid'); // 取本地sessionID
        if(sid){ // 能取到
            wx.request({  // 获取用户信息，同时判断sessionID是否有效
                url: 'https://www.mayizhihuan.com/User/User/userinfo',
                data: {
                    PHPSESSID: sid
                },
                method: 'POST',
                success: function(res) {
                    if (res.data.status == 200){ // 能通过sessionId 取到用户信息, session有效
                        cb && cb(res.data.userInfo)
                    }else{ // session 过期
                        that.userLogin(cb)
                    }
                }
            })
        }else{ // 取不到
            that.userLogin(cb)
        }   
    },
    userLogin : function(cb) { // 登录
        wx.login({
            success: function(data) {
                if (data.code) {
                    wx.getUserInfo({
                        success: function (res) {
                            var wxinfo = res;
                            wxinfo.code = data.code;
                            wxinfo.PHPSESSID = wx.getStorageSync('sid');
                            wxinfo.usertype =1;
                            wx.request({
                                url: 'https://www.mayizhihuan.com/User/Index/wxlogin/', //调用登录接口，返回sessionID，userInfo等
                                method: 'POST',
                                header: {
                                    'content-type': 'application/x-www-form-urlencoded'
                                },
                                data: wxinfo,
                                success: function (res2) {
                                    if(res2.data.status == 1){
                                        wx.setStorageSync('sid', res2.data.sid)
                                        cb && cb(res2.data.userInfo)
                                    }else{
                                        console.log(res2.data.info)
                                    }
                                }
                            })
                        }
                    })
                } else {
                    console.log('获取1用户登录态失败！' + res.errMsg)
                }
            }
        })
    },
    jiazai: function() {
        var that=this;
        wx.request({
            url: 'https://www.mayizhihuan.com/Home/Articlelittle/',
            data: {
                PHPSESSID: wx.getStorageSync('sid')
            },
            method: 'GET',
            header: {
                'content-type': 'application/json'
            },
            success: function(res) {
                that.setData({
                    neirong: res.data.datas
                })
            }
        })
    },
    changeColor: function(res) {
        var that=this;
        var txtArray = this.data.txtArray.slice(0);
        var tL = txtArray.length
        for (var i = 0; i < tL; i++) {
            txtArray[i].changeColor = res.target.id == txtArray[i].id ? true : false;
        }
        this.setData({
            txtArray: txtArray
        })
        wx.request({
            url: 'https://www.mayizhihuan.com/Home/Articlelittle/',
            data: {
                ac_id: res.target.id,
                PHPSESSID: wx.getStorageSync('sid')
            },
            method: 'GET',
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                that.setData({
                    neirong: res.data.datas
                })
            }
        })
    },
    bianse: function(res) {
        var neirong = this.data.neirong.slice(0);
        var tt = neirong.length
        for (var i = 0; i < tt; i++) {
            if (res.target.id == neirong[i].article_id) {
                if (neirong[i].is_share == "true") {
                    neirong[i].is_share = "false"
                    neirong[i].sun--
                } else {
                    neirong[i].is_share = "true"
                    neirong[i].sun++
                }
            }
        }
        this.setData({
            neirong: neirong
        })
        console.log(res.target.id, +"111")
        var that = this
        wx.request({
            url: 'https://www.mayizhihuan.com/Home/Articlelittle/conllection/',
            data: {
                article_id: res.target.id,
                PHPSESSID: wx.getStorageSync('sid')
            },
            method: 'GET',
            header: {
                'content-type': 'application/json'
            },
            success: function(res) {
                console.log(res)
            }
        })
    },
    clickNum: function(e) {
        this.setData({
            _num: e.target.dataset.num
        })
    },
    //事件处理函数
    bindViewTap: function() {
        wx.navigateTo({
            url: '../logs/logs'
        })
    },
    details: function(e) {
        wx.navigateTo({
            url: '../details/details?id=' + e.target.id
        })
        wx.request({
            url: 'https://www.mayizhihuan.com/Home/Articlelittle/',
            data: {
                id: e.target.id,
                PHPSESSID: wx.getStorageSync('sid')
            },
            method: 'GET',
            header: {
                'content-type': 'application/json'
            },
            success: function(res) {
                
            }
        })
    },
    //下拉刷新
    onPullDownRefresh: function() {
        wx.showNavigationBarLoading() //在标题栏中显示加载

        //模拟加载
        setTimeout(function() {
            // complete
            wx.hideNavigationBarLoading() //完成停止加载
            wx.stopPullDownRefresh() //停止下拉刷新
        }, 1500);
    },
    onPullDownRefresh: function() {

    },
    /**
  * 用户点击右上角分享
  */
    onShareAppMessage: function () {

    }
})